package com.android.settings.ios;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.android.settings.ios.view.OsPreference;

public abstract class BaseFragment extends PreferenceFragmentCompat {

    private float mLastAlpha = 0.0f;
    protected String mMainTitle;
    protected String mBackSubTitle;
    protected FragmentActivity mMainActivity;

    public void entryNewScreen(Fragment fragment) {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            if (activity instanceof SettingsMainActivity) {
                ((SettingsMainActivity) activity).updateSubBarBackgroundAlpha(0);
            }
            activity.getSupportFragmentManager().beginTransaction()
                    .setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit, R.anim.fragment_pop_enter, R.anim.fragment_pop_exit)
                    .replace(R.id.fragment_container, fragment)
                    .addToBackStack(null)
                    .commit();
        }
    }

    public void backUp() {
        FragmentActivity activity = getActivity();
        if (activity != null) {
            activity.getSupportFragmentManager().popBackStack();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mBackSubTitle = bundle.getString("fragment_back", null);
            mMainTitle = bundle.getString("fragment_title", getString(R.string.app_name));
            onParseBundle(bundle);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        mMainActivity = getActivity();
        RecyclerView listView = getListView();
        if (listView != null) {
            listView.setItemViewCacheSize(1000);
            listView.addOnScrollListener(new RecyclerView.OnScrollListener() {

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
                    if (lm != null) {
                        View firstView = lm.findViewByPosition(0);
                        if (firstView != null) {
                            if (mLastAlpha != 0.0f) {
                                mLastAlpha = 0.0f;
                                onActionBarScrollChanged(0.0f);
                            }
                        } else {
                            if (mLastAlpha != 1.0f) {
                                mLastAlpha = 1.0f;
                                onActionBarScrollChanged(1.0f);
                            }
                        }
                    }
                }
            });
        }
        return view;
    }

    protected void onParseBundle(Bundle bundle) {

    }

    protected void onActionBarScrollChanged(float alpha) {
        if (mMainActivity instanceof SettingsMainActivity) {
            ((SettingsMainActivity) mMainActivity).updateSubBarBackgroundAlpha(Math.abs(alpha));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateTitleIfNeeded();
    }

    protected void updateTitleIfNeeded() {
        Activity activity = getActivity();
        if (activity instanceof SettingsMainActivity) {
            activity.setTitle(mMainTitle);
            ((SettingsMainActivity) activity).setLeftBackText(mBackSubTitle);
        }
    }

    protected void onPrepareNewFragmentArgs(Preference preference, Bundle bundle) {
        // Used to pass different parameters when opening a new fragment.
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference.getFragment() != null) {
            boolean handled = false;
            Fragment callbackFragment = this;
            while (!handled && callbackFragment != null) {
                if (callbackFragment instanceof OnPreferenceStartFragmentCallback) {
                    handled = ((OnPreferenceStartFragmentCallback) callbackFragment).onPreferenceStartFragment(this, preference);
                }
                callbackFragment = callbackFragment.getParentFragment();
            }
            if (!handled && getContext() instanceof OnPreferenceStartFragmentCallback) {
                handled = ((OnPreferenceStartFragmentCallback) getContext()).onPreferenceStartFragment(this, preference);
            }

            if (!handled && getActivity() instanceof OnPreferenceStartFragmentCallback) {
                handled = ((OnPreferenceStartFragmentCallback) getActivity()).onPreferenceStartFragment(this, preference);
            }
            if (!handled && preference instanceof OsPreference) {
                OsPreference osPreference = (OsPreference) preference;
                Fragment fragment = osPreference.getSelfFragment();
                if (fragment != null) {
                    handled = true;
                    entryNewScreen(fragment);
                }
            }
            if (!handled) {
                final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                final Bundle args = preference.getExtras();
                final Fragment fragment = fragmentManager.getFragmentFactory().instantiate(requireActivity().getClassLoader(), preference.getFragment());
                args.putString("fragment_back", mMainTitle);
                args.putString("fragment_title", (String) preference.getTitle());
                onPrepareNewFragmentArgs(preference, args);
                fragment.setArguments(args);
                if (preference instanceof OsPreference) {
                    ((OsPreference) preference).setSelfFragment(fragment);
                }
                entryNewScreen(fragment);
            }
            return true;
        } else {
            if (preference instanceof OsPreference) {
                OsPreference osPreference = (OsPreference) preference;
                Fragment fragment = osPreference.getSelfFragment();
                if (fragment != null) {
                    entryNewScreen(fragment);
                } else if (osPreference.getAppArgs() != null) {
                    fragment = new CommonAppFragment();
                    Bundle args = osPreference.getAppArgs();
                    args.putString("fragment_back", mMainTitle);
                    args.putString("fragment_title", (String) preference.getTitle());
                    fragment.setArguments(args);
                    osPreference.setSelfFragment(fragment);
                    entryNewScreen(fragment);
                }
            }
        }
        return false;
    }
}
